﻿#include <QFile>
#include "CCorrectPlcOper.h"
#include "CGlobal.h"
#include "Public.h"

CCorrectPlcOper::CCorrectPlcOper(QString sPointName, CDevBeltRealign* pDev, QWidget *parent /*= nullptr*/)
	:m_sPointGuid(sPointName),m_pDev(pDev)
{
	ui.setupUi(this);
	setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
	this->setWindowTitle(A2T("皮带纠偏"));
	// 获取 Qt 窗口的原生 HWND 句柄
	HWND hwnd = reinterpret_cast<HWND>(this->winId());
	setWindowsTitleBarColor(hwnd, RGB(56, 63, 81));
	//setStyleSheetFile();
	initConnect();
	if (m_pDev != nullptr)
	{
		m_pDevDataBeltRealign = m_pDev->m_pDevData;
	}
	m_sNormalLamp = "min-width:20px;min-height:20px;"
		"max-width:20px;max-height:20px;"
		"border-radius:10px;border:1px solid black;"
		"background:rgb(120, 120, 120);";
	m_sWorkLamp = "min-width:20px;min-height:20px;"
		"max-width:20px;max-height:20px;"
		"border-radius:10px;border:1px solid black;"
		"background:rgb(0, 255, 0);";
	m_sAlarmLamp = "min-width:20px;min-height:20px;"
		"max-width:20px;max-height:20px;"
		"border-radius:10px;border:1px solid black;"
		"background:rgb(255, 0, 0);";
	m_sWarningLamp = "min-width:20px;min-height:20px;"
		"max-width:20px;max-height:20px;"
		"border-radius:10px;border:1px solid black;"
		"background:rgb(255,255,0);";
	//配置定时器
	m_pTimer = new QTimer(this);
	m_pTimer->setTimerType(Qt::TimerType::PreciseTimer);
	connect(m_pTimer, &QTimer::timeout, this, &CCorrectPlcOper::slotDataRefresh);
	m_pTimer->start(1000);
}

CCorrectPlcOper::~CCorrectPlcOper()
{
	m_pTimer->stop();
}

void CCorrectPlcOper::setStyleSheetFile()
{
	this->setWindowTitle(A2T("皮带纠偏"));
	QString sQssPath = ":/qss/styleSheet.css";
	QFile qss(sQssPath);
	qss.open(QFile::ReadOnly);
	this->setStyleSheet(qss.readAll());
}

void CCorrectPlcOper::initConnect()
{
	connect(ui.pushButtonAutoMode, &QPushButton::clicked, this, &CCorrectPlcOper::slotAutoMode);//自动模式
	connect(ui.pushButtonManualMode, &QPushButton::clicked, this, &CCorrectPlcOper::slotManualMode);//手动模式
	connect(ui.pushButtonManualLeftRotate, &QPushButton::pressed, this, &CCorrectPlcOper::slotManualLeftRotateP);//控制PLC左转
	connect(ui.pushButtonManualLeftRotate, &QPushButton::released, this, &CCorrectPlcOper::slotManualLeftRotateR);//松开控制PLC左转
	connect(ui.pushButtonManualRightRotate, &QPushButton::pressed, this, &CCorrectPlcOper::slotManualRightRotateP);//控制PLC右转
	connect(ui.pushButtonManualRightRotate, &QPushButton::released, this, &CCorrectPlcOper::slotManualRightRotateR);//松开控制PLC右转
}

void CCorrectPlcOper::slotDataRefresh()
{
	if (m_pDev == nullptr)
		return;
	if (m_pDevDataBeltRealign == nullptr)
		return;
	m_pDevDataBeltRealign->m_pMutex->lock();
	//PLC连接状态
	if (m_pDevDataBeltRealign->m_bPlcConnectState)
	{
		ui.labelPlcComFeedback->setStyleSheet(m_sWorkLamp);
	}
	else
	{
		ui.labelPlcComFeedback->setStyleSheet(m_sAlarmLamp);
	}
	//纠偏远程反馈
	if (m_pDevDataBeltRealign->m_nRecRemoteCtlFeedback == 1)
	{
		ui.labelRectRemFeedback->setStyleSheet(m_sWorkLamp);
	}
	else
	{
		ui.labelRectRemFeedback->setStyleSheet(m_sNormalLamp);
	}
	//纠偏本地反馈
	if (m_pDevDataBeltRealign->m_nRecLocalCtlFeedback == 1)
	{
		ui.labelRecLocalFeedback->setStyleSheet(m_sWorkLamp);
	}
	else
	{
		ui.labelRecLocalFeedback->setStyleSheet(m_sNormalLamp);
	}
	//相机左偏反馈
	if (m_pDevDataBeltRealign->m_nRecCamerLeft == 1)
	{
		ui.labelLeftRecFeedback->setStyleSheet(m_sWorkLamp);
	}
	else
	{
		ui.labelLeftRecFeedback->setStyleSheet(m_sNormalLamp);
	}
	//相机右偏反馈
	if (m_pDevDataBeltRealign->m_nRecCamerRight == 1)
	{
		ui.labelRightRecFeedback->setStyleSheet(m_sWorkLamp);
	}
	else
	{
		ui.labelRightRecFeedback->setStyleSheet(m_sNormalLamp);
	}
	//自动纠偏停止
	if (m_pDevDataBeltRealign->m_nRecErrorAlarm == 1)
	{
		ui.labelAutoRecStop->setStyleSheet(m_sWorkLamp);
	}
	else
	{
		ui.labelAutoRecStop->setStyleSheet(m_sNormalLamp);
	}
	//本地左偏运行
	if (m_pDevDataBeltRealign->m_nRecLocalLeftRun == 1)
	{
		ui.labelLeftRecRun->setStyleSheet(m_sWorkLamp);
	}
	else
	{
		ui.labelLeftRecRun->setStyleSheet(m_sNormalLamp);
	}
	//本地右偏运行
	if (m_pDevDataBeltRealign->m_nRecLocalRightRun == 1)
	{
		ui.labelRightRecRun->setStyleSheet(m_sWorkLamp);
	}
	else
	{
		ui.labelRightRecRun->setStyleSheet(m_sNormalLamp);
	}
	//托辊电机运行
	if (m_pDevDataBeltRealign->m_nRecMotorRunFeedback == 1)
	{
		ui.labelLdlerMotorRun->setStyleSheet(m_sWorkLamp);
	}
	else
	{
		ui.labelLdlerMotorRun->setStyleSheet(m_sNormalLamp);
	}
	//左偏YV运行
	if (m_pDevDataBeltRealign->m_nRecLeftYVRunFeedback == 1)
	{
		ui.labelLeftRecYvRun->setStyleSheet(m_sWorkLamp);
	}
	else
	{
		ui.labelLeftRecYvRun->setStyleSheet(m_sNormalLamp);
	}
	//右偏YV运行
	if (m_pDevDataBeltRealign->m_nRecRightYVRunFeedback == 1)
	{
		ui.labelRightRecYvRun->setStyleSheet(m_sWorkLamp);
	}
	else
	{
		ui.labelRightRecYvRun->setStyleSheet(m_sNormalLamp);
	}
	//m_nRecOffsetValue
	ui.labelOffset->setText(A2T("偏移%1mm").arg(m_pDevDataBeltRealign->m_nRecOffsetValue));

	//自动按钮和手动按钮的使能
	if (m_pDevDataBeltRealign->m_nRecRemoteAuto == 1 && m_pDevDataBeltRealign->m_nRecRemoteHand == 0)
	{
		ui.pushButtonAutoMode->setEnabled(true);
		ui.pushButtonManualMode->setEnabled(true);
		ui.pushButtonAutoMode->setIsTagVisible(true);
		ui.pushButtonManualMode->setIsTagVisible(false);
		ui.pushButtonManualLeftRotate->setEnabled(false);
		ui.pushButtonManualRightRotate->setEnabled(false);
	}
	else if (m_pDevDataBeltRealign->m_nRecRemoteAuto == 0 && m_pDevDataBeltRealign->m_nRecRemoteHand == 1)
	{
		ui.pushButtonAutoMode->setEnabled(true);
		ui.pushButtonManualMode->setEnabled(true);
		ui.pushButtonAutoMode->setIsTagVisible(false);
		ui.pushButtonManualMode->setIsTagVisible(true);
		ui.pushButtonManualLeftRotate->setEnabled(true);
		ui.pushButtonManualRightRotate->setEnabled(true);
		ui.pushButtonManualLeftRotate->setEnabled(true);
		ui.pushButtonManualRightRotate->setEnabled(true);
	}
	else
	{
		ui.pushButtonAutoMode->setEnabled(false);
		ui.pushButtonManualMode->setEnabled(false);
	}
	m_pDevDataBeltRealign->m_pMutex->unlock();
}

void CCorrectPlcOper::slotAutoMode()
{
	SCtrInfo info;
	info.m_sGuid = m_sPointGuid;
	info.m_nCtrCmd = enCSetAuto;
	info.m_nCtrDevType = enOntology;
	info.m_bCtrSync = true;
	m_pDev->ctrDev(info);
}

void CCorrectPlcOper::slotManualMode()
{
	SCtrInfo info;
	info.m_sGuid = m_sPointGuid;
	info.m_nCtrCmd = enCSetMananl;
	info.m_nCtrDevType = enOntology;
	info.m_bCtrSync = true;
	m_pDev->ctrDev(info);
}

void CCorrectPlcOper::slotManualLeftRotateP()
{
	SCtrInfo info;
	info.m_sGuid = m_sPointGuid;
	info.m_nCtrCmd = enCMalForeward;
	info.m_nCtrDevType = enOntology;
	info.m_bCtrSync = true;
	m_pDev->ctrDev(info);
}

void CCorrectPlcOper::slotManualLeftRotateR()
{
	SCtrInfo info;
	info.m_sGuid = m_sPointGuid;
	info.m_nCtrCmd = enCMalForewardStop;
	info.m_nCtrDevType = enOntology;
	info.m_bCtrSync = true;
	m_pDev->ctrDev(info);
}

void CCorrectPlcOper::slotManualRightRotateP()
{
	SCtrInfo info;
	info.m_sGuid = m_pDevDataBeltRealign->m_sPointGuid;
	info.m_nCtrCmd = enCMalRevesal;
	info.m_nCtrDevType = enOntology;
	info.m_bCtrSync = true;
	m_pDev->ctrDev(info);
}

void CCorrectPlcOper::slotManualRightRotateR()
{
	SCtrInfo info;
	info.m_sGuid = m_pDevDataBeltRealign->m_sPointGuid;
	info.m_nCtrCmd = enCMalRevesalStop;
	info.m_nCtrDevType = enOntology;
	info.m_bCtrSync = true;
	m_pDev->ctrDev(info);
}

void CCorrectPlcOper::refreshData(bool bRefresh)
{
	if (bRefresh)
	{
		m_pTimer->start(1000);
	}
	else
	{
		m_pTimer->stop();
	}
}
